package SERVLET;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import BO.AutorBO;
import BO.ObraBO;
import BO.UsuarioBO;
import DTO.AutorDTO;
import DTO.ObraDTO;
import DTO.UsuarioDTO;

public class HomeServlet extends HttpServlet{
	
    public static HashMap<String, String> paginas = new HashMap<String, String>();
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-control", "no-cache, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "Fri, 25 Dec 1980 00:00:00 GMT");

        carregarPaginas();
        
        if (request.getParameter("acao") != null) {
        	if(request.getParameter("acao").equals("Logar")){
        		logar(request, response);
        	} else if (request.getParameter("acao").equals("Deslogar")){
        		deslogar(request, response);
        	} else if (request.getParameter("acao").equals("Usuario")) {
        		request.getSession().setAttribute("usuarios", new UsuarioBO().pesquisarTodos());
                removerAtributo(request);
                request.getRequestDispatcher(paginas.get("usuarioPesquisar")).forward(request, response);            
            } else if (request.getParameter("acao").equals("Obra")) {
            	request.getSession().setAttribute("obras", new ObraBO().pesquisarTodosLivros());
                removerAtributo(request);
                request.getRequestDispatcher(paginas.get("obraPesquisar")).forward(request, response);                
            } else if (request.getParameter("acao").equals("Colecao")) {
            	request.getSession().setAttribute("colecoes", new ObraBO().pesquisarTodasColecoes());
                removerAtributo(request);
                request.getRequestDispatcher(paginas.get("colecaoPesquisar")).forward(request, response);
            } else if (request.getParameter("acao").equals("Autor")) {
            	request.getSession().setAttribute("autores", new AutorBO().pesquisarTodos());
                removerAtributo(request);
                request.getRequestDispatcher(paginas.get("autorPesquisar")).forward(request, response);
            } else if (request.getParameter("acao").equals("Pesquisar")) {
            	
            	if(request.getParameter("pesquisaHome") != null) {
	            	if(request.getParameter("pesquisaHome").equals("autor")) {
	            		request.getRequestDispatcher("AutorServlet").forward(request, response);
	            	} else if(request.getParameter("pesquisaHome").equals("obra")) {
	            		request.getRequestDispatcher("ObraServlet").forward(request, response);
	            	} else if(request.getParameter("pesquisaHome").equals("usuario")) {
	            		request.getRequestDispatcher("UsuarioServlet").forward(request, response);
	            	} else if(request.getParameter("pesquisaHome").equals("colecao")) {
	            		request.getRequestDispatcher("ColecaoServlet").forward(request, response);
	            	}	            	
            	}
            	
        	} else {
                removerAtributo(request);
                request.getRequestDispatcher(paginas.get("home")).forward(request, response);
            }
        }
    }

    private void deslogar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	request.getSession().removeAttribute("usuarioLogado");		        	
    	request.getRequestDispatcher(paginas.get("index")).forward(request, response);
    }
    
    private void logar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	UsuarioDTO usuarioLogado = new UsuarioDTO();  	
    	usuarioLogado.setLogin(request.getParameter("login"));
    	usuarioLogado.setSenha(request.getParameter("senha"));        		
    	usuarioLogado = new UsuarioBO().logar(usuarioLogado);        		
    	request.getSession().setAttribute("usuarioLogado", usuarioLogado);		        	
    	request.getRequestDispatcher(paginas.get("index")).forward(request, response);	
    }
    
    private void removerAtributo(HttpServletRequest request){
        request.getSession().removeAttribute("erro");
    }    
    
    private void carregarPaginas(){
    	paginas.put("index", "/index.jsp");
        paginas.put("home", "pages/home.jsp");
        paginas.put("lista", "pages/lista/listaVisualizar.jsp");
        paginas.put("usuarioPesquisar", "pages/usuario/usuarioPesquisar.jsp");
        paginas.put("usuarioIncluirAlterar", "pages/usuario/usuarioIncluirAlterar.jsp");
        paginas.put("obraPesquisar", "pages/obra/obraPesquisar.jsp");
        paginas.put("obraIncluirAlterar", "pages/obra/obraIncluirAlterar.jsp");
        paginas.put("obraVisualizar", "pages/obra/obraVisualizar.jsp");
        paginas.put("colecaoPesquisar", "pages/colecao/colecaoPesquisar.jsp");
        paginas.put("colecaoIncluirAlterar", "pages/colecao/colecaoIncluirAlterar.jsp");
        paginas.put("colecaoVisualizar", "pages/colecao/colecaoVisualizar.jsp");
        paginas.put("autorPesquisar", "pages/autor/autorPesquisar.jsp");
        paginas.put("autorIncluirAlterar", "pages/autor/autorIncluirAlterar.jsp");
        paginas.put("autorVisualizar", "pages/autor/autorVisualizar.jsp");        
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
	public 	List<ObraDTO> getObrasMaisLidas() {
		return new ObraBO().pesquisarObrasMaisLidas(); 
    }
	
	public 	List<ObraDTO> getObrasMelhoresAvaliadas() {
		return new ObraBO().pesquisarObrasDestaque();
	}

	public 	List<AutorDTO> getAutoresDestaque() {
		return new AutorBO().pesquisar4AutoresAleatorios();
	}
}